﻿using System;
using System.Threading;
using XcoAppSpaces.Contracts.Logging;
using XcoAppSpaces.Transport.WCF;
using NUnit.Framework;
using XcoAppSpaces.Contracts.Transport;
using XcoAppSpaces.Serialization.Binary;

namespace test.XcoAppSpaces.Transport.WCF.nettcp
{
	[TestFixture]
	public class testXcoWCFTransportService
	{
        private XcoBinarySerializer serializer = new XcoBinarySerializer();
        private ReceivedTmp recv1;
        private ReceivedTmp recv2 ;
	

		[SetUp]
		public void SetUp()
		{
            recv1 = new ReceivedTmp();
            recv2 = new ReceivedTmp();
			XcoLoggerFactory.CreateInstance = (t => new ConsoleLogger());
		}


		[Test]
		public void Connect()
		{
            recv1.WaitForStop = new ManualResetEvent(false);
            recv2.WaitForStop = new ManualResetEvent(false);
			using (XcoWCFTransportService wcf1 = new XcoWCFTransportService(8123))
			{
				using (XcoWCFTransportService wcf2 = new XcoWCFTransportService(8124))
				{
					wcf1.OnMessageReceived += recv1.MessageReceived;
					wcf2.OnMessageReceived += recv2.MessageReceived;
					
					wcf1.Start();
					wcf2.Start();

                    byte[] data = serializer.Serialize(new IntMsg { value = 5 });
                    byte[] data2 = serializer.Serialize(new IntMsg { value = 3 });

                    wcf2.Send(new XcoMessage { Content = data }, wcf1.LocalAddress);
					wcf1.Send(new XcoMessage { Content = data2 }, wcf2.LocalAddress);

                    recv1.WaitForStop.WaitOne(1000);
                    recv2.WaitForStop.WaitOne(1000);

					wcf1.Stop();
					wcf2.Stop();
                    Assert.AreEqual(recv1.Sum, 5);
                    Assert.AreEqual(recv2.Sum, 3);
					
				}
			}
		}

		[Test]
		public void SharedPort()
		{
            recv1.WaitForStop = new ManualResetEvent(false);
            recv2.WaitForStop = new ManualResetEvent(false);

			int samePort = 8123;
			using (XcoWCFTransportService wcf1 = new XcoWCFTransportService(samePort))
			{
				using (XcoWCFTransportService wcf2 = new XcoWCFTransportService(samePort))
				{
					//wcf1.SetLogger(new TestLoggerFactory());
					//wcf2.SetLogger(new TestLoggerFactory());

					wcf1.SharedPortName = "Process1";
					wcf2.SharedPortName = "Process2";

					wcf1.OnMessageReceived += recv1.MessageReceived;
					wcf2.OnMessageReceived += recv2.MessageReceived;
					
					wcf1.Start();
					wcf2.Start();

                    byte[] data = serializer.Serialize(new IntMsg { value = 5 });
                    byte[] data2 = serializer.Serialize(new IntMsg { value = 3 });

					wcf2.Send(new XcoMessage { Content = data }, wcf1.LocalAddress);
					wcf1.Send(new XcoMessage { Content = data2 }, wcf2.LocalAddress);

                    recv1.WaitForStop.WaitOne(1000);
                    recv2.WaitForStop.WaitOne(1000);

					wcf1.Stop();
					wcf2.Stop();
					Assert.AreEqual(recv1.Sum, 5);
                    Assert.AreEqual(recv2.Sum, 3);

				}
			}
		}
	}


}
